#include "mySort.h"
#include <iostream>
#include <string>

MySort::MySort(){
	num = 0;
	origin = NULL;
}

MySort::MySort(int num, int* origin){
	this->num = num;
	this->origin = origin;
}

MySort::~MySort(){
	num = 0;
	origin = NULL;
}

void MySort::changeSwap(int& a, int& b){
	int temp = a;
	a = b;
	b = temp;
}

string MySort::printNum(){
	string pnum = "";
	for (int i = 0; i < num - 1; i++)
	{
		pnum += to_string(origin[i]);
		pnum += " ";
	}
	pnum += to_string(origin[num-1]);
	pnum += "\n";
	return pnum;
}

string MySort::paritySort(){
	string pnum = "";
	int k, flag1, flag2;
	flag1 = 1;
	k = 1;
	while (1) {
		flag2 = 0;
		for (int i = k; i < num - 1; i += 2) {
			if (origin[i] > origin[i + 1])
			{
				changeSwap(origin[i], origin[i + 1]);
				pnum += printNum();
				flag2 = 1;
			}
		}
		if (flag1 == 0 && flag2 == 0) 
			return pnum;
		else 
		{
			flag1 = flag2;
			k = (k + 1) % 2;
		}
	}
}

string MySort::bubbleSort(){
	string bnum = "";
	for (int i = 0; i < num - 1; i++)
	{
		for (int j = 0; j < num - i - 1; j++)
		{
			if (origin[j] > origin[j + 1])
			{
				changeSwap(origin[j], origin[j + 1]);
				bnum += printNum();
			}
		}
	}
	return bnum;
}
